﻿package 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import Socket.*;
	import Component.ScreenSwap;
	import Component.LoadScreen;
	import Component.ConnectionScreen;
	import flash.geom.ColorTransform;
	import Component.Slab;

	public class Main extends MovieClip
	{
		public function Main()
		{
			var context:Context = new Context();
			var slab:Slab = new Slab();
			// load components
			context.screen = new ScreenSwap(this);

			var loader:LoadScreen = new LoadScreen();
			context.screen.load_screen = loader;
			loader.context = context;
			loader.width = width;
			loader.height = height;

			var connection_screen:ConnectionScreen = new ConnectionScreen();
			context.screen.connection_screen = connection_screen;

			context.screen.display_connection_screen();

			context.connection = new ConnectionWatcher(context);
			connection_screen.boutonConnect.addEventListener(MouseEvent.CLICK,context.connection.connect);

			context.listener = new ActionListener(context);
			context.sender = new ActionSender(context);
			
			context.sender.key.bind(connection_screen.InputPlayerID);
			context.connection.address.bind(connection_screen.InputServerAdress);
			context.connection.port.bind(connection_screen.InputServerPort);

			context.input = new InputManager(context);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, context.input.keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, context.input.keyUpHandler);
			stage.addEventListener(Event.ENTER_FRAME, context.input.enterFrame);

			// count params
			var parameters:Object = this.root.loaderInfo.parameters;
			var nb_parameters:int = 0;
			for(var i:String in parameters) nb_parameters++;
			
			if(nb_parameters != 3) return;
			//context.map = map;
			connection_screen.InputPlayerID.text = parameters.id;
			connection_screen.InputServerAdress.text = parameters.address;
			connection_screen.InputServerPort.text = parameters.port;
			
			context.connection.connect(new Event(""));
		}
	}

}